package com.awanp3.nimbus.base.service.impl;

import com.awanp3.nimbus.common.core.utils.MapstructUtils;
import com.awanp3.nimbus.common.core.utils.StringUtils;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.awanp3.nimbus.base.domain.bo.BaseLocationBo;
import com.awanp3.nimbus.base.domain.vo.BaseLocationVo;
import com.awanp3.nimbus.base.domain.BaseLocation;
import com.awanp3.nimbus.base.mapper.BaseLocationMapper;
import com.awanp3.nimbus.base.service.IBaseLocationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 库位信息Service业务层处理
 *
 * @author 20014170
 * @date 2025-09-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BaseLocationServiceImpl implements IBaseLocationService {

    private final BaseLocationMapper baseMapper;

    /**
     * 查询库位信息
     *
     * @param locationId 主键
     * @return 库位信息
     */
    @Override
    public BaseLocationVo queryById(Long locationId){
        return baseMapper.selectVoById(locationId);
    }

    /**
     * 分页查询库位信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 库位信息分页列表
     */
    @Override
    public TableDataInfo<BaseLocationVo> queryPageList(BaseLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseLocation> lqw = buildQueryWrapper(bo);
        Page<BaseLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的库位信息列表
     *
     * @param bo 查询条件
     * @return 库位信息列表
     */
    @Override
    public List<BaseLocationVo> queryList(BaseLocationBo bo) {
        LambdaQueryWrapper<BaseLocation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseLocation> buildQueryWrapper(BaseLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseLocation> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BaseLocation::getLocationId);
        lqw.eq(StringUtils.isNotBlank(bo.getLocationCode()), BaseLocation::getLocationCode, bo.getLocationCode());
        lqw.like(StringUtils.isNotBlank(bo.getLocationName()), BaseLocation::getLocationName, bo.getLocationName());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationType()), BaseLocation::getLocationType, bo.getLocationType());
        lqw.eq(StringUtils.isNotBlank(bo.getPlantCode()), BaseLocation::getPlantCode, bo.getPlantCode());
        lqw.eq(bo.getLength() != null, BaseLocation::getLength, bo.getLength());
        lqw.eq(bo.getWidth() != null, BaseLocation::getWidth, bo.getWidth());
        lqw.eq(bo.getHeight() != null, BaseLocation::getHeight, bo.getHeight());
        lqw.eq(bo.getVolumeLimit() != null, BaseLocation::getVolumeLimit, bo.getVolumeLimit());
        lqw.eq(bo.getWeightLimit() != null, BaseLocation::getWeightLimit, bo.getWeightLimit());
        lqw.eq(bo.getBoxLimit() != null, BaseLocation::getBoxLimit, bo.getBoxLimit());
        lqw.eq(bo.getSkuLimit() != null, BaseLocation::getSkuLimit, bo.getSkuLimit());
        lqw.eq(bo.getPalletLimit() != null, BaseLocation::getPalletLimit, bo.getPalletLimit());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BaseLocation::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增库位信息
     *
     * @param bo 库位信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseLocationBo bo) {
        BaseLocation add = MapstructUtils.convert(bo, BaseLocation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLocationId(add.getLocationId());
        }
        return flag;
    }

    /**
     * 修改库位信息
     *
     * @param bo 库位信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseLocationBo bo) {
        BaseLocation update = MapstructUtils.convert(bo, BaseLocation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseLocation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除库位信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
